﻿using NAudio.Wave;

namespace RFIDStation.DeviceDriver.Services;

public partial class AudioService
{
    private static readonly object o = new object( );

    private readonly ILogger _logger;

    private WaveOutEvent _device;
    private AudioFileReader _reader;

    public AudioService( ILoggerFactory loggerFactory )
    {
        _logger = loggerFactory.CreateLogger( nameof( AudioService ) );

    }

    public void DisposeAll( )
    {
        _reader?.Dispose( );
        _device?.Dispose( );
    }

    public void Ready( string file )
    {
        this.DisposeAll( );

        _device = new WaveOutEvent( );
        _reader = new AudioFileReader( file );

        _device.Init( _reader );
    }

    public void Play( bool reset = true )
    {
        if ( reset ) _reader.Position = 0;

        _device?.Play( );

        Thread.Sleep( 100 );
    }
}
